package cn.ljh.zard.io;


import cn.ljh.zard.parse.Parse;
import cn.ljh.zard.parse.ParseException;
import cn.ljh.zard.parse.ParseRegister;
import java.io.InputStream;
import java.util.Collection;
import java.util.Optional;

/**
 * 默认的资源加载器
 */
public class DefaultResourceLoader implements IResourceLoader {


    // 解析器
    private ParseRegister ParseRegister;

    public DefaultResourceLoader(ParseRegister parseRegister) {
        ParseRegister = parseRegister;
    }

    @Override
    public InputStream getIS(String location) {
        Optional.<String>ofNullable(location).orElseThrow(() -> new NotFoundFileException("没有文件路径"));

        Collection<Parse> parses = ParseRegister.getParses();
        for (Parse parse : parses) {
            InputStream resource = parse.parse(location);
            if (resource != null) {
                return resource;
            }
        }

        throw new
                ParseException("无法处理目录下的文件" + location);
    }
}
